<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20231021
 * Time: 11:05 
 * 资产管理
 */
namespace app\finance\controller;

use  Mscore\Core\Controller;
use  app\finance\model\Withdraw as WithdrawModel;
use  app\finance\model\Recharge as RechargeModel;
use  app\finance\model\Examine as ExamineModel;
use  app\finance\model\Common as CommonModel;
use  Mscore\Core\Lib\ArrayMethod;
use  app\admin\model\Login as LoginModel;
use  Mscore\Core\Lang;

class ExamineController extends Controller{

	private $model;
    private $login;
    private $withdraw;
    private $recharge;
    /**
     * 初始化自动连接方法
     * @param array $sysmod 
     * @param array $where 
    */
    public function __construct()  
    {    
        $this->withdraw = new WithdrawModel;
        $this->recharge = new RechargeModel;
        $this->login = (new LoginModel)->isAuth();
        if(isset($this->login['code'])&&$this->login['code']==401){
            echo json_encode($this->login);
            Header("Location:".config('server.login_url'));
            exit;
        }
        $this->language = isset($_COOKIE['language'])?$_COOKIE['language']:'default';
        Lang::init($this->language);
        $this->success=Lang::get('Success');
        $this->error=Lang::get('Error');
    }   

    
    /**
     * 审核列表
     * @param array $list 
     * @param array $where 
     */ 
    public function list(){
        $action = get('action');
        $this->assign('action', $action);
        $this->display('list.html');
    }

    /**
     * 审核列表
     * @param array $recharge 
     * @param array $where 
     */ 
    public function recharge(){
        $action = get('action');
        $this->assign('action', $action);
        $this->display('recharge.html');
    }

    /**
     * 充值列表
     * @param array $rechargelist 
     * @param array $where 
     */ 
    public function rechargelist(){  
        $page = post('page',1);
        $limit = post('limit',10);
        $aid = !empty(post('id'))?post('id'):get('id');
        $where = 'a.dels<1 ';
        if($this->login['info']['ranges']<1){
            $where .= ' and a.pid = '.$this->login['info']['aid'].' or a.aid='.$this->login['info']['aid'];
        }
        if(post('status')){
            $where .= ' and a.status = '.post('status');
        }
        if(post('username')){
            $where .= ' and b.username = "'.post('username').'"';
        }
        if(post('starttime')&&post('endtime')){
            $starttime = post('starttime');
            $endtime = post('endtime');
            $where .= " and (a.createtime BETWEEN '".$starttime."' AND '".$endtime."' ) ";
        }
        $datalist = $this->recharge->list($where,$page,$limit);
        $data = [
            "code" => 200,
            "data" => $datalist['datalist'],
            "count"=> $datalist['total'],
            "msg" => 'ok'
        ];
        $this->echojson($data);
    }

    /**
     * 提现列表
     * @param array $withdraw 
     * @param array $where 
     */ 
    public function withdraw(){  
        $page = post('page',1);
        $limit = post('limit',10);
        $where = 'a.dels<1 and a.type < 2';
        if($this->login['info']['ranges']<1){
            $where .= ' and a.pid = '.$this->login['info']['aid'].' or a.aid='.$this->login['info']['aid'];
        }
        if(post('status')){
            $where .= ' and a.status = '.post('status');
        }
        if(post('username')){
            $where .= ' and b.username = "'.post('username').'"';
        }
        if(post('starttime')&&post('endtime')){
            $starttime = post('starttime');
            $endtime = post('endtime');
            $where .= " and (a.createtime BETWEEN '".$starttime."' AND '".$endtime."' ) ";
        }
        $datalist = $this->withdraw->withdraw($where,$page,$limit);
        $data = [
            "code" => 200,
            "data" => $datalist['datalist'],
            "count"=> $datalist['total'],
            "msg" => 'ok'
        ];
        $this->echojson($data);
    }

    /**
     * 提现审核
     * @param array $draw 
     * @param array $where 
     */
    public function draw(){ 
        $params = post();
        if($params){
            //post请求则保存
            $status = (new ExamineModel())->dexamine($params, $this->login['info']);
            switch ($status) {
                case true:
                    $this->echoJson($this->success);
                    break;
                default:
                    $this->echoJson($this->error);
                    break;
            }
        }else{
            if(get('id')){
                $datainfo =$this->withdraw->drawinfo(get('id'));
                $user=(new CommonModel())->getAccount('aid='.$datainfo['aid'],'amount,integra');
            }else{
                $datainfo = [];
            }
            $bank = (new CommonModel())->bank(' aid='.$datainfo['aid']);
            $this->assign('bank', $bank);
            $this->assign('user', $user);
            $this->assign('datainfo',$datainfo);
            $this->display('draw.html');
        }
    }

    /**
     * 充值审核
     * @param array $rechargesh 
     * @param array $where 
     */
    public function rechargesh(){ 
        $params = post();
        if($params){
            //post请求则保存
            $status = (new ExamineModel())->examine($params, $this->login['info']);
            switch ($status) {
                case true:
                    $this->echoJson($this->success);
                    break;
                default:
                    $this->echoJson($this->error);
                    break;
            }
        }else{
            if(get('id')){
                $datainfo =$this->recharge->recharge(get('id'));
            }else{
                $datainfo = [];
            }
            $this->assign('datainfo', $datainfo);
            $this->display('rechargesh.html');
        }
    }

}
